package org.icress.fusion.moc

class TaController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
    def taService

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 100, 100)
        render view : '/moc/ta/list', model : [taInstanceList: taService.list(params), taInstanceTotal: taService.count(params)]
    }

    def create = {
        def taInstance = new Ta()
        taInstance.properties = params
        render view : '/moc/ta/create', model : [taInstance: taInstance]
    }

    def save = {
        def taInstance = new Ta(params)
        if (taService.save(taInstance, params, true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'moc.ta.label', default: 'Ta'), taInstance.id])}"
            redirect(action: "show", id: taInstance.id)
        }else {
            render(view: "/moc/ta/create", model: [taInstance: taInstance])
        }
    }

    def show = {
        def taInstance = taService.get(params.id)
        if (!taInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.ta.label', default: 'Ta'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/moc/ta/show', model : [taInstance: taInstance]
        }
    }

    def edit = {
        def taInstance = taService.get(params.id)
        if (!taInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.ta.label', default: 'Ta'), params.id])}"
            redirect(action: "list")
        }else {
            render view : '/moc/ta/edit', model : [taInstance: taInstance]
        }
    }

    def update = {
        def taInstance = taService.get(params.id)
        if(!taInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.ta.label', default: 'Ta'), params.id])}"
            redirect(action: "list")
            return
        }
        
        if (params.version) {
            def version = params.version.toLong()
            if (taInstance.version > version) {
                taInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'moc.ta.label', default: 'Ta')] as Object[], "Another user has updated this Ta while you were editing")
                render(view: "/moc/ta/edit", model: [taInstance: taInstance])
                return
            }
        }

        taInstance.properties = params
        if (!taInstance.hasErrors() && taService.save(taInstance, params, true)) {
            flash.message = "${message(code: 'default.updated.message', args: [message(code: 'moc.ta.label', default: 'Ta'), taInstance.id])}"
            redirect(action: "show", id: taInstance.id)
        }
        else {
            render(view: "/moc/ta/edit", model: [taInstance: taInstance])
        }
    }

    def delete = {
        def taInstance = taService.get(params.id)
        if (!taInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.ta.label', default: 'Ta'), params.id])}"
            redirect(action: "list")
            return
        }
        
        try {
            taService.delete(taInstance, true)
            flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'moc.ta.label', default: 'Ta'), params.id])}"
            redirect(action: "list")
        }catch (org.springframework.dao.DataIntegrityViolationException e) {
            flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'moc.ta.label', default: 'Ta'), params.id])}"
            redirect(action: "show", id: params.id)
        }
    }
}
